using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;

public enum MobGeneratorType {
	Mob = 0,
	Chest = 1,

}

public class MobGenerator:MonoBehaviour, IGenerationComponent {

	public static Dictionary<int,SortedDictionary<float,MobGenerator>> instances = new Dictionary<int,SortedDictionary<float,MobGenerator>>();

	public MobGeneratorType type;
	public float priority;
	public float priorityRange = 1f;

	public void OnGeneration() {
		if(!instances.ContainsKey((int)type)) instances.Add((int)type,new SortedDictionary<float,MobGenerator>());
		var addTo = instances[(int)type];
		addTo.Add(Random.Range(priority-priorityRange,priority+priorityRange),this);
	}


	public delegate void GenerateCallBack(Vector2 position,int grade);
	public static void Generate(int targetType,GenerateCallBack generateCallBack,int[] counts) {
		if(!instances.ContainsKey(targetType)) return;
		SortedDictionary<float,MobGenerator> toGenerate = instances[targetType];

		var currentGenerator = toGenerate.GetEnumerator();

		try {
			for(int currentGrade = 0;currentGrade<counts.Length;currentGrade++) {
				for(int i = 0;i<counts[currentGrade];i++) {

					if(!currentGenerator.MoveNext())
						throw new System.Exception();
					generateCallBack(currentGenerator.Current.Value.transform.position,currentGrade);

				}
			}
		} catch(System.Exception e) { }
		toGenerate.Clear();
	}

}

